package com.fifth.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.HeadNav;
import com.fifth.common.pojo.entity.Zone;
import com.fifth.home.mapper.ZoneMapper;
import com.fifth.home.service.IZoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 专区 服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-01
 */
@Service
public class ZoneServiceImpl extends ServiceImpl<ZoneMapper, Zone> implements IZoneService {
    @Resource
    private ZoneMapper zoneMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 专区 模糊查询
     * @param
     * @return
     */
    @Override
    public CommonResult fuzzyQuery(Zone zone, Integer currentPage, Integer pageSize) {

        IPage<Zone> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(zone.getZoneName()),"zone_name",zone.getZoneName());
        wrapper.eq(StringUtils.isNotBlank(zone.getIsDisable()),"is_disable",zone.getIsDisable());
        wrapper.eq("is_deleted","0");
        wrapper.orderByAsc("sort");

        IPage<Zone> zoneIPage = zoneMapper.selectPage(page, wrapper);

        return new CommonResult(2000,"查询成功",zoneIPage);
    }

    /**
     * 前端显示专区
     * @return
     */
    @Override
    public  CommonResult prefectureSearch() {
        if (redisTemplate.hasKey("prefecture")) {
            //查看redis是否存在prefecture
            List<HeadNav> list = JSON.parseObject(redisTemplate.opsForValue().get("prefecture"), List.class);
            return new CommonResult(2000, "专区查询成功", list);

        } else {
            QueryWrapper<Zone> wrapper = new QueryWrapper<>();
            wrapper.eq("is_deleted","0");
            wrapper.eq("is_disable","0");
            wrapper.orderByAsc("sort");
            List<Zone> zones = zoneMapper.selectList(wrapper);
            //List<Zone> zones = zoneMapper.selectAll();

            String s = JSON.toJSONString(zones);
            //System.out.println(s);
            redisTemplate.opsForValue().set("prefecture", s,24, TimeUnit.HOURS);
            return new CommonResult(2000, "专区查询成功", zones);
        }
    }

    /**
     * 新增专区
     * @param zone
     * @return
     */
    @Override
    public CommonResult addPrefecture(Zone zone) {
        int insert = zoneMapper.insert(zone);

        String prefecture = redisTemplate.opsForValue().get("prefecture");
        if(prefecture!=null && !"".equals(prefecture)){
            Boolean head1 = redisTemplate.delete("prefecture");
        }
        return new CommonResult(2000,"添加专区成功",null);
    }

    /**
     * 修改专区信息
     * @param zone
     * @return
     */
    @Override
    public CommonResult updatePrefectureDetails(Zone zone) {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("id", zone.getId());
        Zone zone1 = new Zone();
        zone1.setZoneName(zone.getZoneName());
        zone1.setSort(zone.getSort());
        zone1.setZoneDesc(zone.getZoneDesc());

        int update = zoneMapper.update(zone1, wrapper);

        String head = redisTemplate.opsForValue().get("prefecture");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("prefecture");
        }
        return new CommonResult(2000, "修改专区信息成功", null);
    }

    /**
     * 删除专区
     * @param id
     * @return
     */
    @Override
    public CommonResult deletePrefecture(String id) {
        int i = zoneMapper.deleteById(id);

        String prefecture = redisTemplate.opsForValue().get("prefecture");
        if(prefecture!=null && !"".equals(prefecture)){
            Boolean head1 = redisTemplate.delete("prefecture");
        }
        return new CommonResult(2000, "删除专区成功", null);
    }

    /**
     * 修改专区状态  0启用 1未启用
     * @param id
     * @param isDisable
     * @return
     */
    @Override
    public CommonResult updateStatus(String id, String isDisable) {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Zone zone = new Zone();
        zone.setIsDisable(isDisable);
        int update = zoneMapper.update(zone, wrapper);
        String head = redisTemplate.opsForValue().get("prefecture");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("prefecture");
        }
        return new CommonResult(2000, "修改状态成功", null);
    }


}
